// pages/myPages/personalCenter/phoneNumber/changePhoneNumber/changePhoneNumber.js
var request = require('./request.js')

//获取应用实例
const app = getApp()
var interval = null
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: true,
    userNameInput: '',
    userN: '',
    View: false,
    date: '请选择日期',
    fun_id: 2,
    time: wx.T.locale == "zh-Hans" ? wx.T.locales['zh-Hans'].GetCode : wx.T.locales['en'].GetCode, //倒计时 
    currentTime: 60,
    identifyingCode: [],
    Length: 6,        //输入框个数  
    isFocus: false,    //聚焦  
    Value: "",        //输入的内容  
    ispassword: false, //是否密文显示 true为密文， false为明文。
    MobilePhoneNo: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  webNatPage: function (e) {
    var current = e.currentTarget.dataset.current;
    wx.navigateTo({
      url: '../../../setPage/webNatPage/webNatPage?id=' + current
    })
  },
  userNameInput: function (e) {
    this.setData({
      userN: e.detail.value,
      MobilePhoneNo: e.detail.value
    })
    this.loginBtnClick();
  },
  loginBtnClick: function () {
    if (this.data.userN.length != 0) {
      this.setData({
        show: false
      })
    } else {
      this.setData({
        show: true
      })
    }
  },
  getCode: function (options) {
    var that = this;
    var currentTime = that.data.currentTime
    interval = setInterval(function () {
      currentTime--;
      that.setData({
        time: currentTime + 's后重新获取'
      })
      if (currentTime <= 0) {
        clearInterval(interval)
        that.setData({
          time: wx.T.locale == "zh-Hans" ? wx.T.locales['zh-Hans'].GetCode : wx.T.locales['en'].GetCode,
          currentTime: 60,
          disabled: false
        })
      }
    }, 1000)
  },
  onChangeShowState: function (e) {
    if(this.data.show == false){
      var that = this;
      if (/^\d{11}$/.test(this.data.MobilePhoneNo)){
           that.setData({
            View: (!that.data.View),
            Value: ''
          })
      }else{
        wx.showToast({
          // title: '手机号输入有误',
          title: wx.T.locale == "zh-Hans" ? wx.T.locales['zh-Hans'].NumberJudge : wx.T.locales['en'].NumberJudge,
          icon: 'none',
          duration:1000,
          mask: true
        });
      }
     
    } else {
      wx.showToast({
        title: wx.T.locale == "zh-Hans" ? wx.T.locales['zh-Hans'].Notempty : wx.T.locales['en'].Notempty,
        icon: 'none',
        duration: 1000,
        mask: true
      });
    }
  },
  getVerificationCode() {
    var that = this
    var data = {
      access_token: wx.getStorageSync('createUser').access_token,
      mobile_phone: that.data.MobilePhoneNo,
    };
    request.getMobileVarcode(data, function (d) {
      console.log(d)
      wx.showToast({
        title: d.message,
        icon: 'succes',
        duration: 500,
        mask: true
      });
      that.getCode();
      that.setData({
        time: '60s后重新获取',
        disabled: true
      })
    });
  },
  Focus(e) {
    var that = this;
    var inputValue = e.detail.value;
    that.setData({
      Value: inputValue,
      isFocus: true
    })
    if (inputValue.length == 6) {
      var data = {
        access_token: "4dTl2756Gaq-EzW0dFXXWRRZJ7muxocA_1517070195",
        mobile_phone: that.data.MobilePhoneNo,
        varcode: inputValue
      };
      request.bindMobileVa(data, function (d) {
        console.log(d)
        if (d.code == 200) {
          if (that.data.View) {
            that.setData({
              View: (!that.data.View),
              Value: '',
            })
          }
          wx.navigateBack({
            delta: 2
          })
        }
        wx.showToast({
          title: d.message,
          icon: 'succes',
          duration: 500,
          mask: true
        });
      });
    }
  },
  bindPhoneNumber: function () {

  },
  Tap() {
    var that = this;
    that.setData({
      isFocus: true,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      setLang: wx.T.locale == "zh-Hans" ? wx.T.locales['zh-Hans'] : wx.T.locales['en']

    })
    wx.setNavigationBarTitle({
      title: wx.T.locale == "zh-Hans" ? wx.T.locales['zh-Hans'].ModifyPhoneNumber : wx.T.locales['en'].ModifyPhoneNumber
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})